package com.zzyl.serve.service.impl;

import java.util.List;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zzyl.serve.domain.NursingProjectPlan;
import com.zzyl.serve.dto.NursingPlanDto;
import com.zzyl.serve.mapper.NursingProjectPlanMapper;
import com.zzyl.serve.vo.NursingPlanVo;
import com.zzyl.serve.vo.NursingProjectPlanVo;
import com.zzyl.serve.service.INursingProjectPlanService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import com.zzyl.serve.mapper.NursingPlanMapper;
import com.zzyl.serve.domain.NursingPlan;
import com.zzyl.serve.service.INursingPlanService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.stream.Collectors;

/**
 * 护理计划Service业务层处理
 *
 * @author ruoyi
 * @date 2024-10-04
 */
@Transactional
@Service
public class NursingPlanServiceImpl extends ServiceImpl<NursingPlanMapper, NursingPlan> implements INursingPlanService {
    @Autowired
    private NursingPlanMapper nursingPlanMapper;

    @Autowired
    private INursingProjectPlanService nursingProjectPlanService;

    /**
     * 查询护理计划
     *
     * @param id 护理计划主键
     * @return 护理计划
     */
    @Override
    public NursingPlanVo selectNursingPlanById(Long id) {

        //1.查询护理计划主表数据
        NursingPlan nursingPlan = getById(id);

        //2.查询护理计划关联的 护理项目子表数据
        List<NursingProjectPlan> list = nursingProjectPlanService.lambdaQuery()
                .eq(NursingProjectPlan::getPlanId, id)
                .list();//但是这里面拿到的数据 和我需要的数据格式不一致
        //这里用stream流转换成NursingProjectPlanVo
        List<NursingProjectPlanVo> projectPlanVoList = list.stream().map(projectPlan -> {
            NursingProjectPlanVo projectPlanVo = BeanUtil.toBean(projectPlan, NursingProjectPlanVo.class);
            projectPlanVo.setProjectId(String.valueOf(projectPlan.getProjectId()));
            projectPlanVo.setExecuteCycle(String.valueOf(projectPlan.getExecuteCycle()));
            projectPlanVo.setExecuteFrequency((projectPlan.getExecuteFrequency().longValue()));
            return projectPlanVo;
        }).collect(Collectors.toList());//因为要转多个对象，所以这里使用{}


        //3.构建NursingPlanVo 返回
        NursingPlanVo nursingPlanVo = BeanUtil.toBean(nursingPlan, NursingPlanVo.class);
        nursingPlanVo.setProjectPlans(projectPlanVoList);
        return nursingPlanVo;

    }

    /**
     * 查询护理计划列表
     *
     * @param nursingPlan 护理计划
     * @return 护理计划
     */
    @Override
    public List<NursingPlan> selectNursingPlanList(NursingPlan nursingPlan) {
        return nursingPlanMapper.selectNursingPlanList(nursingPlan);
    }

    @Autowired
    private NursingProjectPlanMapper nursingProjectPlanMapper;

    /**
     * 新增护理计划
     *
     * @param nursingPlanDto 护理计划
     * @return 结果
     */
    @Transactional
    @Override
    public int insertNursingPlan(NursingPlanDto nursingPlanDto) {
        //1.保存护理计划主表数据
        NursingPlan nursingPlan = BeanUtil.toBean(nursingPlanDto, NursingPlan.class);
        this.save(nursingPlan);//MyBatisPlus的save保存完数据后会将主键id回填到对象中

        //2.批量保存护理计划项目 子表数据
        List<NursingProjectPlan> projectPlans = nursingPlanDto.getProjectPlans();
        if (ObjectUtil.isEmpty(projectPlans)) {
            return 1;
        }
        projectPlans.forEach(projectPlan -> projectPlan.setPlanId(nursingPlan.getId()));//主表刚生成的id，要回填到子表对象中
        boolean result = this.nursingProjectPlanService.saveBatch(projectPlans);

        // 3、返回结果
        return result ? 1 : 0;
    }

    /**
     * 修改护理计划
     *
     * @param nursingPlanDto 护理计划
     * @return 结果
     */
    @Override
    public int updateNursingPlan(NursingPlanDto nursingPlanDto) {
        //1.修改护理计划关联的护理项目子表
        //1.1 先删除护理计划关联的护理项目子表数据
        LambdaQueryWrapper<NursingProjectPlan> eq = Wrappers.<NursingProjectPlan>lambdaQuery().eq(NursingProjectPlan::getPlanId, nursingPlanDto.getId());
        this.nursingProjectPlanService.remove(eq);

        //1.2判断前端是否有提交子表数据，如果有，则批量保存
        List<NursingProjectPlan> projectPlans = nursingPlanDto.getProjectPlans();
        if (ObjectUtil.isNotEmpty(projectPlans)) {
            projectPlans.forEach(projectPlan -> projectPlan.setPlanId(nursingPlanDto.getId()));
            this.nursingProjectPlanService.saveBatch(projectPlans);
        }

        //2.修改护理计划主表数据
        NursingPlan nursingPlan = BeanUtil.toBean(nursingPlanDto, NursingPlan.class);
        return this.updateById(nursingPlan) ? 1 : 0;

    }

    /**
     * 批量删除护理计划
     *
     * @param ids 需要删除的护理计划主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteNursingPlanByIds(Long[] ids) {
        // 1.批量删除护理计划关联的护理项目
        // delete from nursing_project_plan where plan_id in (1,2,3)
        nursingProjectPlanService.remove(Wrappers.<NursingProjectPlan>lambdaQuery().in(NursingProjectPlan::getPlanId, ids));

        // 2.删除护理计划基本信息
        return removeByIds(Arrays.asList(ids)) ? 1 : 0;
    }


    /**
     * 删除护理计划信息
     *
     * @param id 护理计划主键
     * @return 结果
     */
    @Override
    public int deleteNursingPlanById(Long id) {
        // 删除护理计划与护理项目的关系
        nursingProjectPlanMapper.deleteByPlanId(id);
        // 删除护理计划
        return removeById(id) ? 1 : 0;
    }
}
